package com.hig.onlineexam.controller;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hig.onlineexam.domain.dto.ExamQuestionsContentData;
import com.hig.onlineexam.domain.dto.ExamQuestionsData;
import com.hig.onlineexam.service.IExamQuestionsContentDataService;
import com.hig.questions.domain.ExamQuestionsProperty;
import com.hig.questions.mapper.ExamQuestionsPropertyMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hig.onlineexam.domain.dto.ExamQuestionsListData;
import com.hig.onlineexam.service.IExamQuestionsListDataService;
import com.ruoyi.common.core.domain.AjaxResult;

@RestController
@RequestMapping("/onlineexam/questionslist")
@Api("考试关联试题列表")
public class ExamQuestionsListDataController {
	
	@Autowired
	IExamQuestionsListDataService examQuestionsListDataService;

    @Autowired
    private ExamQuestionsPropertyMapper propertyMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IExamQuestionsContentDataService examQuestionsContentDataService;
	@GetMapping()
    @ApiOperation("获取试题列表")

    public AjaxResult getQuestionsList(ExamQuestionsListData examQuestionsListData)
    {
        String no = examQuestionsListData.getNo();
        String examCode = examQuestionsListData.getExamCode();

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String userCode = loginUser.getUser().getUserCode();
        List<ExamQuestionsListData> cacheList = redisCache.getCacheList("questionList:" + userCode + examCode);
        if (CollectionUtil.isEmpty(cacheList)){
            List<ExamQuestionsListData> examQuestionsList = examQuestionsListDataService.selectQuestionsList(examQuestionsListData);
//            for (ExamQuestionsListData questionsListData : examQuestionsList) {
//                String questionsCode = questionsListData.getQuestionsCode();
////            String examCode = questionsListData.getExamCode();
//                ExamQuestionsProperty examQuestionsProperty = propertyMapper.selectExamQuestionsPropertyById(questionsCode);
//                questionsListData.setRightAnswer(examQuestionsProperty.getRightAnswer());
//        }
            for (ExamQuestionsListData questionsListData : examQuestionsList) {
                String questionsCode = questionsListData.getQuestionsCode();
                ExamQuestionsContentData examQuestionsContentData = new ExamQuestionsContentData();
                examQuestionsContentData.setExamCode(examCode);
                examQuestionsContentData.setQuestionsCode(questionsCode);
                ExamQuestionsData examQuestionsData = examQuestionsContentDataService.convertQuestion(examQuestionsContentData);
                questionsListData.setRightAnswer(examQuestionsData.getExamQuestionsContentData().getRightAnswer());
            }

            redisCache.setCacheList("questionList:" + userCode + examCode,examQuestionsList);
            cacheList=examQuestionsList;
        }
        if (CollectionUtil.isNotEmpty(cacheList)&& StringUtils.isNotEmpty(no)){
            for (ExamQuestionsListData questionsListData : cacheList) {

                String questionsCode = questionsListData.getQuestionsCode();
                ExamQuestionsContentData eqc = new ExamQuestionsContentData();
                eqc.setNo(no);
                eqc.setQuestionsCode(questionsCode);
                eqc.setExamCode(examCode);
                ExamQuestionsData examQuestionsData = examQuestionsContentDataService.convertQuestion(eqc);
                if (ObjectUtil.isNotNull(examQuestionsData)){
                    ExamQuestionsContentData examQuestionsContentData = examQuestionsData.getExamQuestionsContentData();
                    questionsListData.setQuestionsAnswer(examQuestionsContentData.getUserAnswer());
                }

            }
        }

        return AjaxResult.success(cacheList);
    }
}
